package Lab12;

import java.io.*;

public class Ex12_2 {
    public static void main(String[] args) throws IOException {
        String encoding = "GBK";
//    https://www.cnblogs.com/manongxiaojiang/archive/2012/10/13/2722068.html
        String filePath = "src/Lab12/testdata.txt";
        File file = new File(filePath);
        int lineCnt = 0;
        double total = 0;
        if (file.isFile() && file.exists()) { //判断文件是否存在
            InputStreamReader read = new InputStreamReader(
                    new FileInputStream(file), encoding);//考虑到编码格式
            BufferedReader bufferedReader = new BufferedReader(read);

            String lineTxt = null;
            while ((lineTxt = bufferedReader.readLine()) != null) {

                total += Double.parseDouble(lineTxt);
//          System.out.println(lineTxt);
                lineCnt++;
            }
            read.close();
        } else {
            System.out.println("找不到指定的文件");
        }

        System.out.println("Total is " + total);
        System.out.println("Average is " + total / lineCnt);
    }
}
